package cn.itcast.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;

@Controller
@RequestMapping("user") //模块
public class UserController {

	@Autowired
	private UserService userService ;
	
	/**
	 * 查询所有用户
	 * @param model
	 * @return
	 */
	@RequestMapping("findAll")
	public String findAll(Model model){
		List<User> list = userService.findAll();
		model.addAttribute("users", list);
		
		return "users";
	} 
	
	/**
	 * 添加跳转到添加页面的方法
	 */
	@RequestMapping("toAdd")
	public String toAdd(){
		return "userAdd";
	}
	
	/**
	 * 保存用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("save")
	public String save(User user){
		
		userService.insert(user);
		
		return "redirect:/user/findAll";
	}
	
}
